package com.beauty.dressing.module.community.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;

import com.beauty.dressing.LBApplication;
import com.beauty.dressing.R;
import com.facebook.drawee.view.SimpleDraweeView;
import com.framework.common.utils.ICommonUtil;
import com.framework.common.utils.IDisplayUtil;
import com.framework.common.utils.IFileUtil;
import com.framework.common.utils.ILog;
import com.xiaolanba.commonlib.fresco.FrescoUtil;

import java.util.ArrayList;

/**
 * @author bond
 * @createtime 2015年12月31日 16:05
 * @company 9zhitx.com
 * @description
 */

public class ImageAdapter extends BaseAdapter {
    private final String TAG = getClass().getSimpleName();
    private Context context;
    private Listener listener;
    private ArrayList<String> imageList;
    private int width;
    private int height;
    private boolean isCropImageWidth = true;
    public void setListener(Listener listener) {
        this.listener = listener;
    }
    private int screenWidth;

    /**
     * 设置取七牛缩略图时，是裁剪还是完全展示(目前帖子动态只有一张图时，要完全展示)
     * @param flag
     */
    public void setCropImageWidth(boolean flag){
        this.isCropImageWidth = flag;
    }

    private String longUrl;
    public void setLongPicUrl(String url){
        this.longUrl = url;
    }

    public ImageAdapter(ArrayList<String> imageList, Context context, int widths, int heights) {
        this.context = context;
        this.imageList = imageList;
        this.listener = null;
        width = widths;
        height = heights;
    }

    public ImageAdapter(ArrayList<String> imageList, Listener listener, Context context) {
        this(context, imageList, listener, null, false);
    }

    public ImageAdapter(Context context, ArrayList<String> imageList, Listener listener, GridView gridView, boolean adjustWidthBySize) {
        if (imageList == null) {
            ILog.w("ImageAdapter", "imagesStr is empty!");
            return;
        }
        if (context == null) {
            context = LBApplication.getInstance();
        }
        isCropImageWidth = true;
        longUrl = null;
        this.context = context;
        this.imageList = imageList;
        this.listener = listener;
        int space = IDisplayUtil.dip2px(context,5);
        int padding = IDisplayUtil.dip2px(context,12);
        int count = getCount();
        int factor = adjustWidthBySize && count > 0 && count < 3 ? count : 3;
        if (adjustWidthBySize && gridView != null) {
            gridView.setNumColumns(factor);
        }
        width = (IDisplayUtil.getScreenWidth(context) - (space * (factor - 1)) - (padding * 2)) / factor;
        height=width;
    }

    /**
     * 微博动态的构造方法(4个item当成2个item计算宽度)
     * @param imageList
     * @param listener
     * @param context
     * @param isInnerGrid
     */
    public ImageAdapter(GridView gridView, ArrayList<String> imageList, Listener listener, Context context, boolean isInnerGrid, boolean adjustWidthBySize) {
        if (imageList == null) {
            ILog.w("ImageAdapter", "imagesStr is empty!");
            return;
        }
        if (context == null) {
            context = LBApplication.getInstance();
        }
        screenWidth = IDisplayUtil.getScreenWidth(context);
        longUrl = null;
        this.context = context;
        this.imageList = imageList;
        this.listener = listener;
        int count = getCount();
        int gridFullWidth = 0;
        int factor = adjustWidthBySize && count > 0 && count < 3 ? count : 3;
        int space = IDisplayUtil.dip2px(context,5);
        int padding = IDisplayUtil.dip2px(context,5);
        if (!isInnerGrid){
            width = (IDisplayUtil.getScreenWidth(context) - (space * (factor-1)) - (padding * 2))/ factor;
            gridFullWidth = screenWidth - IDisplayUtil.dip2px(context,12*2);
        }else{
            width = (IDisplayUtil.getScreenWidth(context) - (space * (factor-1)) - (padding * 2)- IDisplayUtil.dip2px(context,8*2))/ factor;
            gridFullWidth = screenWidth - IDisplayUtil.dip2px(context,(12+8)*2);
        }
        if (adjustWidthBySize && gridView != null) {
            if (count==4){
                gridView.setNumColumns(2);
                gridView.getLayoutParams().width = width*2 + space;
            }else{
                gridView.setNumColumns(factor);
                gridView.getLayoutParams().width = gridFullWidth;
            }
        }
        if (factor == 1) {
            height = (int)(width/1.33f);
            isCropImageWidth = false;
        } else {
            height = width;
            isCropImageWidth = true;
        }

    }

    @Override
    public int getCount() {
        return null == imageList ? 0 : imageList.size();
    }

    @Override
    public String getItem(int position) {
        return imageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder holder;
        if (null == convertView) {
            convertView = LayoutInflater.from(LBApplication.getInstance()).inflate(R.layout.adapter_img_gv, parent, false);
            holder = new MyViewHolder();
            holder.frameLayout = (FrameLayout) convertView.findViewById(R.id.frame_layout);
            holder.img = (SimpleDraweeView) convertView.findViewById(R.id.img);
            holder.frameLayout.getLayoutParams().width = width;
            holder.frameLayout.getLayoutParams().height = height;
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        String url = getItem(position);
        String smallUrl;
            smallUrl = ICommonUtil.getThumbImageUrlNotCrop(url, width, height);
        FrescoUtil.loadImage(holder.img, smallUrl, false);
        holder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
        holder.img.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.onClickImage(position, imageList);
                } else {
                    if (null != context && context instanceof Activity) {
//                        ImageShowActivity.startActivity((Activity) context, imageList, position, true, ThumbOption.getDynamicThumbOption());
                    }
                }
            }
        });
        return convertView;
    }

    private static class MyViewHolder {
        public SimpleDraweeView img;
        public FrameLayout frameLayout;
    }

    public interface Listener {
        void onClickImage(int position, ArrayList<String> imageList);
    }
}
